﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package Classes {
    import flash.events.*;
    import flash.geom.*;
    import flash.utils.*;
    import org.aswing.*;
    import Classes.util.*;
    import Classes.effects.*;
    import Display.FactoryUI.*;
    import GameMode.*;
    import Classes.doobers.*;
    import Classes.actions.*;
    import Modules.peddler.*;
    import Display.peddlerUI.peddlerMarketUI.data.*;
    import Display.peddlerUI.*;
    import Engine.*;

    public class Peddler extends Business {

        private const PELR:String = "peddler";

        protected var m_isListeningToRender:Boolean;
        private var m_pdlrModel:PeddlerModel;
        private var m_cryoutTimer:Timer;
        private var m_cryoutTip:PeddlerCryoutToolTip;
        private var m_cryoutMessage:String;
        private var m_peddlerId:String;
        private var m_isOpen:Boolean;
        private var m_closeBuilding:MapResource;
        private var m_pdlrQuitEffect:PeddlerQuitAnimationEffect;

        public function Peddler(_arg1:String=null){
            super(_arg1);
            m_objectType = WorldObjectTypes.PEDDLER;
            m_typeName = this.PELR;
            this.m_isOpen = true;
            this.createPeddlerQuitEffect();
        }
        override public function loadObject(_arg1:Object):void{
            super.loadObject(_arg1);
            this.m_cryoutMessage = _arg1.message;
            if (m_pick){
                m_pick.visible = true;
            };
        }
        public function setPick(_arg1:SlidePick):void{
            this.removePick();
            m_pick = _arg1;
            this.attachPick();
            this.changeState();
        }
        private function changeState():void{
            var _local2:String;
            var _local1:PeddlerData = Global.peddlerManager.getModelById(this.peddlerId);
            if (_local1){
                _local1.counting_time = GlobalEngine.getTimer();
                _local2 = Global.getVisiting();
                _local1.status = HarvestableResource.STATE_PLANTED;
                _local1.hostId = _local2;
                PeddlerBar.getInstance().upDateCell(_local1);
                Global.ui.m_friendBar.addPeddlerIconForFriend(_local2);
            };
        }
        public function removePick():void{
            if (((m_pick) && (m_pick.parent))){
                m_pick.parent.removeChild(m_pick);
                this.cleanUpPick();
            };
            m_pick = null;
            this.removeRenderListeners();
        }
        private function attachPick():void{
            var _local1:PeddlerSlidePick;
            if (((((m_pick) && (!((m_pick.parent == m_displayObject))))) && (!(GlobalEngine.viewport.hudBase.contains(m_pick))))){
                _local1 = (m_pick as PeddlerSlidePick);
                _local1.updatePercentages();
                GlobalEngine.viewport.hudBase.addChild(m_pick);
                this.randomlyDisplayCryout();
                this.addRenderListener();
                m_isHighlightable = true;
            };
        }
        protected function setPickPosition():void{
            var _local1:Point = new Point(-30, -135);
            var _local2:Point = IsoMath.tilePosToPixelPos(m_position.x, m_position.y);
            _local2 = IsoMath.viewportToStage(_local2);
            if (m_pick){
                m_pick.x = (_local2.x + _local1.x);
                m_pick.y = (_local2.y + _local1.y);
            };
            if (this.m_cryoutTip){
                this.m_cryoutTip.x = ((_local2.x + _local1.x) + 25);
                this.m_cryoutTip.y = ((_local2.y + _local1.y) - this.m_cryoutTip.height);
            };
        }
        private function randomlyDisplayCryout():void{
            if (!this.m_cryoutTip){
                this.m_cryoutTip = new PeddlerCryoutToolTip();
                this.m_cryoutTip.setCryoutText(this.m_cryoutMessage);
                GlobalEngine.viewport.hudBase.addChild(this.m_cryoutTip);
            };
            if (!this.m_cryoutTimer){
                this.m_cryoutTimer = new Timer(3000);
                this.m_cryoutTimer.addEventListener(TimerEvent.TIMER, this.onCryoutTimer);
                this.m_cryoutTimer.start();
            };
        }
        private function onCryoutTimer(_arg1:TimerEvent):void{
            this.m_cryoutTip.visible = !(this.m_cryoutTip.visible);
        }
        protected function addRenderListener():void{
            if (((!(this.m_isListeningToRender)) && (!((Global.stage == null))))){
                Global.stage.addEventListener(Event.RENDER, this.renderHandler, false, 0, true);
                this.m_isListeningToRender = true;
                this.setPickPosition();
            };
        }
        protected function removeRenderListeners():void{
            if (Global.stage != null){
                Global.stage.removeEventListener(Event.RENDER, this.renderHandler);
            };
            if (this.m_cryoutTimer){
                this.m_cryoutTimer.stop();
                this.m_cryoutTimer.removeEventListener(TimerEvent.TIMER, this.onCryoutTimer);
                this.m_cryoutTimer = null;
            };
            if (((this.m_cryoutTip) && (this.m_cryoutTip.parent))){
                this.m_cryoutTip.parent.removeChild(this.m_cryoutTip);
                this.m_cryoutTip = null;
            };
            this.m_isListeningToRender = false;
        }
        protected function renderHandler(_arg1:Event):void{
            this.setPickPosition();
        }
        private function cleanUpPick():void{
            if (((m_pick) && (m_pick.hasOwnProperty("cleanUp")))){
                var _local1 = m_pick;
                _local1["cleanUp"]();
            };
        }
        override protected function createCoinPickEffect():void{
            if (!m_coinPickEffect){
                m_coinPickEffect = (MapResourceEffectFactory.createEffect(this, EffectType.COIN_PICK) as CoinPickEffect);
                m_coinPickEffect.pickOffsetX = 8;
                m_coinPickEffect.pickOffsetY = -90;
                m_coinPickEffect.setPickType(CoinPickEffect.PICK_1);
            } else {
                m_coinPickEffect.pickOffsetX = 8;
                m_coinPickEffect.pickOffsetY = -90;
                m_coinPickEffect.setPickType(CoinPickEffect.PICK_1);
                m_coinPickEffect.reattach();
            };
        }
        private function createPeddlerQuitEffect():void{
            if (!this.m_pdlrQuitEffect){
                this.m_pdlrQuitEffect = new PeddlerQuitAnimationEffect(this, (this.m_itemName + "_quit"), true);
                this.m_pdlrQuitEffect.preLoadAnimation();
            } else {
                this.m_pdlrQuitEffect.reattach();
            };
        }
        override public function performVisitAnimation(_arg1:Peep):void{
            if (this.isOpen()){
                super.performVisitAnimation(_arg1);
            };
        }
        override public function isVisitorInteractable():Boolean{
            return (false);
        }
        override public function isHarvestable():Boolean{
            return (false);
        }
        override public function getToolTipHeader():String{
            return (getFranchiseName());
        }
        override public function getCustomToolTipStatus():Component{
            return (null);
        }
        override public function getToolTipStatus():String{
            return (null);
        }
        override protected function updateState(_arg1:Number):Boolean{
            if (this.isHarvestable()){
                return (true);
            };
            removeStagePickEffect();
            return (false);
        }
        override public function isFranchise():Boolean{
            return (false);
        }
        public function set peddlerModel(_arg1:PeddlerModel):void{
            this.m_pdlrModel = _arg1;
        }
        public function get peddlerModel():PeddlerModel{
            return (this.m_pdlrModel);
        }
        public function setIsOpenStatus(_arg1:Boolean):void{
            this.m_isOpen = _arg1;
        }
        override public function isOpen():Boolean{
            return (this.m_isOpen);
        }
        override public function onVisitPlayAction():void{
        }
        public function set cryoutMessage(_arg1:String):void{
            this.m_cryoutMessage = _arg1;
        }
        public function get peddlerId():String{
            return (this.m_peddlerId);
        }
        public function set peddlerId(_arg1:String):void{
            this.m_peddlerId = _arg1;
        }
        public function onHarvest():void{
            if (this.getId() <= 0){
                return;
            };
            if (m_pick){
                if (m_content){
                    m_content.visible = false;
                };
                this.createPeddlerQuitEffect();
                m_actionQueue.removeAllStates();
                m_actionQueue.addActions(new ActionProgressBar(null, this, ZLoc.t("Main", "BusinessHarvesting"), Global.gameSettings.getNumber("actionBarHarvest")), new ActionFn(this, this.onHarvestCompleteFun));
            };
        }
        public function onExpel():void{
            if (this.getId() <= 0){
                return;
            };
            if (m_pick){
                if (m_content){
                    m_content.visible = false;
                };
                this.createPeddlerQuitEffect();
                m_actionQueue.removeAllStates();
                m_actionQueue.addActions(new ActionProgressBar(null, this, ZLoc.t("Main", "BusinessHarvesting"), Global.gameSettings.getNumber("actionBarHarvest")), new ActionFn(this, this.onExpelCompleteFun));
            };
        }
        private function onExpelCompleteFun():void{
            GameTransactionManager.addTransaction(new TExpelPeddler(this));
            this.harvestOrExpelComplete();
        }
        private function onHarvestCompleteFun():void{
            GameTransactionManager.addTransaction(new THarvestPeddler(this));
            this.harvestOrExpelComplete();
        }
        private function harvestOrExpelComplete():void{
            var _local1:PeddlerSlidePick = (m_pick as PeddlerSlidePick);
            m_doobersArray = this.makeDoobers(_local1.moneyEarned);
            spawnDoobers();
            this.disablePeddler();
            this.removePick();
            this.removePeddlerFromWorld();
            Global.ui.m_friendBar.hidePeddlerIcon(Global.getVisiting());
            var _local2:PeddlerModel = Global.peddlerManager.getModelById(this.peddlerModel.peddlerId);
            if (_local2){
                _local2.status = PeddlerModel.STATE_REST;
            };
            PeddlerBar.getInstance().upDateCell(_local2);
        }
        override protected function makeDoobers(_arg1:Number=1):Array{
            var _local2:Array = new Array();
            _local2.push(new Array(Global.gameSettings.getDooberFromType(Doober.DOOBER_COIN, _arg1), _arg1));
            return (_local2);
        }
        override public function onPlayAction():void{
            if (m_pick){
                (m_pick as PeddlerSlidePick).onMouseClick();
            };
        }
        public function onAccept():void{
            GameTransactionManager.addTransaction(new TPermitPeddler(this));
            this.m_pdlrModel.allowed = true;
        }
        override public function getBusinessOwner():String{
            if (this.m_pdlrModel){
                return (this.m_pdlrModel.ownerId);
            };
            return (Global.player.uid);
        }
        override public function sell():void{
        }
        override public function onSell():void{
        }
        override public function getToolTipAction():String{
            return ("");
        }
        override public function getCursor():Class{
            return (null);
        }
        override public function getCustomCursor():Object{
            return (null);
        }
        public function disablePeddler():void{
            if (m_content){
                m_content.visible = false;
            };
            this.m_isOpen = false;
            setActive(false);
        }
        public function removePeddlerFromWorld():void{
            detach();
            cleanup();
            super.onSell();
        }
        override public function getGameModeToolTipAction():String{
            var _local1:String;
            var _local2:GameMode = Global.world.getTopGameMode();
            if ((_local2 is GMEditMove)){
                if (!this.isMoveLocked){
                    _local1 = ZLoc.t("Main", "Move_ToolTip");
                } else {
                    _local1 = GetNoMoveMessage();
                };
            } else {
                if ((_local2 is GMEditRotate)){
                    _local1 = ZLoc.t("Main", "Rotate_ToolTip");
                } else {
                    if ((_local2 is GMObjectMove)){
                        _local1 = ZLoc.t("Main", "Place_ToolTip");
                    };
                };
            };
            return (_local1);
        }

    }
}//package Classes 
